package com.zgm.ch06.aop.aspectJ.introduce._2_7;

import java.math.BigDecimal;
import java.util.Date;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zgm.ch06.aop.aspectJ.introduce._2_7.interfaces.CallTracker;
import com.zgm.ch06.aop.services.StockService;
import com.zgm.ch06.aop.services.UserService;

public class IntroductionDemo {

	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("com/zgm/ch06/aop/aspectJ/introduce/_2_7/_2_7-context.xml");
		UserService userService = (UserService) ac.getBean("userService");
		describeTracker(userService);
		
		userService.login("zgm");
		userService.setAdministratorUsername("x");
		describeTracker(userService);
		
		StockService stockService = (StockService) ac.getBean("stockService");
		describeTracker(stockService);
		
		try {
			stockService.getStockLevel(null);
			
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		System.out.println(stockService.getStockLevel("ABC"));
		stockService.applyDiscounts(new Date(), new BigDecimal("10.0"));
		describeTracker(stockService);
	}
	
	public static void describeTracker(Object o) {
		CallTracker callTracker = (CallTracker) o;
		
		System.out.println(callTracker.describe());
	}
}
